package com.iflytek.xiri2.hal;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.net.Uri;
import android.util.Log;

/**
 * 硬件抽象层
 * 
 * @author 张华勇
 * 
 */
public class HALUtil {
	private Context mContext;
	private AudioManager mAudioManager;
	public int max_Volume;
	private int curVolume;
	private int mStreamType = AudioManager.STREAM_MUSIC;

	public HALUtil(Context context, int maxVolume) {
		mContext = context;
		mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		max_Volume = mAudioManager.getStreamMaxVolume(mStreamType);
		Log.e(HAL.TAG, "maxvolume==========" + max_Volume);
	}

	/**
	 * 上网
	 * 
	 * @param URL
	 */
	public void search_internet(String URL) {
		Uri uri = Uri.parse(URL);
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		intent.addCategory(Intent.CATEGORY_BROWSABLE);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		mContext.startActivity(intent);
	}

	/**
	 * 设置音量
	 * 
	 * @param volume
	 */
	public void setVolume(int volume) {
		// 这里判断是否说了音量增大或减小
		if (curVolume == volume + 7) {
			Log.e("hal", "set volume=  ----");
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_LOWER,
					AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
		} else if (curVolume == volume - 7) {
			Log.e("hal", "set volume=  +++");
			if (volume == 7) {
				mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 1, AudioManager.FLAG_SHOW_UI);
			}
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_RAISE,
					AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
		} else {
			Log.e("hal", "set volume=" + volume);
//			volume = toSystemVolume(volume);
//			com.jmake.halset.volume
//			intent.putExtra("volume", volume);

			try {
				Intent intent = new Intent("com.jmake.karaoke.halset.volume");
				intent.putExtra("volume", volume);
				mContext.sendBroadcast(intent);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e(HAL.TAG, "JsonError=" + e.getMessage());
			}

//             jsonArray.add(jsonFun);
//             feedBackOk(jsonArray.toString());
			Log.e(HAL.TAG, "set volume:==========" + volume);
//			mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume,
//					AudioManager.FLAG_SHOW_UI);
		}
	}

	/**
	 * 静音
	 * 
	 * @param context
	 */
	int vol = 0;

	public void __mute(Context context) {
		AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		vol = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		vol = vol < 0 ? 0 : vol;
		Log.e(HAL.TAG, vol + "");
		mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_SHOW_UI);
		Log.e(HAL.TAG, "mute");
	}

	/**
	 * 取消静音
	 */
	public void __unmute(Context context) {
		AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		int temp = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		if (temp <= 0) {
			temp = vol;
		}
		temp = temp < 0 ? 0 : temp;
		if (temp >= 0) {
			mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, temp, AudioManager.FLAG_SHOW_UI);
		}
		Log.e(HAL.TAG, "unmute");
	}

	/**
	 * 获取当前音量
	 * 
	 * @param context
	 * @return
	 */
	public int getCurVolume(Context context) {
		AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		curVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		Log.d(HAL.TAG, "get volume= " + curVolume);
		curVolume = toXiriVolume(curVolume);
		Log.d(HAL.TAG, "get volume=toxiri " + curVolume);
		return curVolume;
	}

	/**
	 * 将系统音量转换为语点需要的音量
	 * 
	 * @param systemVolume
	 * @return
	 */
	public int toXiriVolume(int systemVolume) {
		int temp = 100 * systemVolume / max_Volume;
		return temp;
	}

	/**
	 * 将语点音量转换为系统需要的音量
	 * 
	 * @param systemVolume
	 * @return
	 */
	public int toSystemVolume(int xiriVolume) {
		int temp = xiriVolume * max_Volume / 100;
		return temp;
	}
}
